<?php
    session_start();
    require_once '../includes/runtime/lib/Propel.php';
    Propel::init("../includes/build/conf/easylib-conf.php");
    set_include_path("../includes/build/classes" . PATH_SEPARATOR . get_include_path());
    if(isset($_SESSION["admin_login"])&&isset($_SESSION["admin_key"])){
        $query = TabAdminQuery::create()->filterByLogin($_SESSION["admin_login"])->findOne();
        if($query!=null){
            if(md5($query->getPass())==$_SESSION["admin_key"]){
                switch ($_GET["action"]){
                    case "liste":
                        $reparations = TabReparationQuery::create()->orderByDateReparation(Criteria::DESC)->find();
                        $results = array();
                        foreach ($reparations as $reparation){
                            $reparation instanceof TabReparation;
                            $byciclette = $reparation->getTabByciclette();
                            $station = $byciclette->getTabStation();
                            $results[] = array(
                                "id" => $reparation->getIdReparation(),
                                "station" => $station->getStationName(),
                                "date" => $reparation->getDateReparation(),
                                "id_byciclette" => $byciclette->getIdByciclette(),
                                "statut" => $reparation->getStatut()
                            );
                        }
                        echo json_encode($results);
                        break;
                    case "reparer":
                        $reparation = TabReparationQuery::create()->filterByIdReparation($_GET["id"])->findOne();
                        if($reparation!=null){
                            $reparation->delete();
                        }
                        break;
                }
            }
        }
    }
?>
